﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using System.Linq;
namespace WPFExcelReport
{
    //
    public class YCAAB05DOCData : BaseFunction
    {

        INTEXDBC INTEXDBC;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public YCAAB05DOCData()
            : base()
        {
             
        }
        public YCAAB05DOCData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        { 
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        public YCAAB05DOCData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
            
        } 
        
        public DataSet BuildData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            try
            {
                Result = INTEXDBC.QueryABKOPF(dwInput);
                Return.Merge(Result);
                DataRow rowtemp = null;
                #region=======Order======
                foreach (DataRow row in Return.Tables["ABKOPF"].Rows)
                {
                    rowtemp = INTEXDB.ABPOS.NewRow();
                    rowtemp["ABPMANDANT"] = row["ABKMANDANT"];
                    rowtemp["ABPAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["ABPAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryABPOS(rowtemp, 2);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.ABAUFM.NewRow();
                    rowtemp["ABAMANDANT"] = row["ABKMANDANT"];
                    rowtemp["ABAVKAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["ABAVKAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryICC_ABAUFM(rowtemp, 2);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.ABPR.NewRow();
                    rowtemp["ARMANDANT"] = row["ABKMANDANT"];
                    rowtemp["ARAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["ARAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryABPR(rowtemp,4);
                    Return.Merge(Result); 

                    rowtemp = INTEXDB.ABAUFM.NewRow();
                    rowtemp["ABAMANDANT"] = row["ABKMANDANT"];
                    rowtemp["ABAAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["ABAAUFLFD"] = row["ABKAUFLFD"];                    
                    Result = INTEXDBC.QueryABAUFM(rowtemp,4);
                    Return.Merge(Result);

                    rowtemp = INTEXDB.FDECK.NewRow();
                    rowtemp["FDMANDANT"] = row["ABKMANDANT"];
                    rowtemp["FDAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["FDAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryFDECK(rowtemp, 4);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.WDECK.NewRow();
                    rowtemp["WDMANDANT"] = row["ABKMANDANT"];
                    rowtemp["WDAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["WDAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryWDECK(rowtemp, 6);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.MDECK.NewRow();
                    rowtemp["MDMANDANT"] = row["ABKMANDANT"];
                    rowtemp["MDAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["MDAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryMDECK(rowtemp, 4);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.GAAUF.NewRow();
                    rowtemp["GAMANDANT"] = row["ABKMANDANT"];
                    rowtemp["GAAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["GAAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryGAAUF(rowtemp, 4);
                    Return.Merge(Result);
                }
                #endregion
                #region========material==========             
                var query =  Return.Tables["FDECK"].AsEnumerable()
                        .Select(row => new {
                            FDMANDANT = row.Field<string>("FDMANDANT"),
                            FDART = row.Field<string>("FDART"),
                            FDVAR = row.Field<string>("FDVAR")
                         }).Distinct();
                foreach (var row in query)
                {
                    rowtemp = INTEXDB.FSSTAMM.NewRow();
                    rowtemp["FSMANDANT"] = row.FDMANDANT;
                    rowtemp["FSART"] = row.FDART;
                    rowtemp["FSVAR"] = row.FDVAR;
                    Result = INTEXDBC.QueryFSSTAMM(rowtemp, 1);
                    Return.Merge(Result);
                    //FSSTAMM;FSFARBEN;FSSTUELI;FSGROE
                    rowtemp = INTEXDB.FSFARBEN.NewRow();
                    rowtemp["FSFMANDANT"] = row.FDMANDANT; ;
                    rowtemp["FSFART"] = row.FDART;
                    rowtemp["FSFVAR"] = row.FDVAR;
                    Result = INTEXDBC.QueryFSFARBEN(rowtemp, 2);
                    Return.Merge(Result);

                    rowtemp = INTEXDB.FSSTUELI.NewRow();
                    rowtemp["FSSMANDANT"] = row.FDMANDANT; ;
                    rowtemp["FSSART"] = row.FDART;
                    Result = INTEXDBC.QueryFSSTUELI(rowtemp, 2);
                    Return.Merge(Result); 
                }
         var    query2 = Return.Tables["WDECK"].AsEnumerable()
             .Select(row => new        {
                 WDMANDANT = row.Field<string>("WDMANDANT"),
                 WDART = row.Field<string>("WDART"),
                 WDVAR = row.Field<string>("WDVAR")
             }).Distinct();
         foreach (var row in query2)
                {
                    rowtemp = INTEXDB.WASTAMM.NewRow();
                    rowtemp["WAMANDANT"] = row.WDMANDANT;
                    rowtemp["WAART"] = row.WDART;
                    rowtemp["WAVAR"] = row.WDVAR;
                    Result = INTEXDBC.QueryWASTAMM(rowtemp, 2);
                    Return.Merge(Result);
                    //FSSTAMM;FSFARBEN;FSSTUELI;FSGROE
                    rowtemp = INTEXDB.WAFARBEN.NewRow();
                    rowtemp["WAFMANDANT"] = row.WDMANDANT;
                    rowtemp["WAFART"] = row.WDART;
                    rowtemp["WAFVAR"] = row.WDVAR;
                    Result = INTEXDBC.QueryWAFARBEN(rowtemp, 2);
                    Return.Merge(Result);
                }
         var query3 = Return.Tables["MDECK"].AsEnumerable()
             .Select(row => new
             {
                 MDMANDANT = row.Field<string>("MDMANDANT"),
                 MDMAT = row.Field<string>("MDMAT"),
                 MDVAR = row.Field<string>("MDVAR")
             }).Distinct();
         foreach (var row in query3)
                {
                    rowtemp = INTEXDB.MSSTAMM.NewRow();
                    rowtemp["MSMANDANT"] = row.MDMANDANT;
                    rowtemp["MSMAT"] =Left(row.MDMAT,14);
                    rowtemp["MSVAR"] = row.MDVAR;
                    Result = INTEXDBC.QueryMSSTAMM(rowtemp, 1);
                    Return.Merge(Result); 
                }
         var query4 = Return.Tables["GAAUF"].AsEnumerable()
             .Select(row => new
             {
                 GAMANDANT = row.Field<string>("GAMANDANT"),
                 GAART = row.Field<string>("GAART"),
                 GAVAR = row.Field<string>("GAVAR")
             }).Distinct();
         foreach (var row in query4)
                {
                    rowtemp = INTEXDB.GSDAT.NewRow();
                    rowtemp["GSMANDANT"] = row.GAMANDANT;
                    rowtemp["GSMAT"] = row.GAART;
                    rowtemp["GSVAR"] = row.GAVAR;
                    Result = INTEXDBC.QueryGSDAT(rowtemp, 1);
                    Return.Merge(Result);
                }
                #endregion
                #region========external producer==========               
                foreach (DataRow row in Return.Tables["ABPOS"].Rows)
                { 
                    rowtemp = INTEXDB.ABFOLGE.NewRow();
                    rowtemp["ABFMANDANT"] = row["ABPMANDANT"];
                    rowtemp["ABFAUFKRS"] = row["ABPAUFKRS"];
                    rowtemp["ABFAUFLFD"] = row["ABPAUFLFD"];
                    rowtemp["ABFAUFPOS"] = row["ABPAUFPOS"];
                    Result = INTEXDBC.QueryABFOLGE(rowtemp, 2);
                    Return.Merge(Result);
                    string processed = "";
                    foreach(DataRow rowf in Result.Tables[0].Rows)
                    {
                        string ABFLIEFNR = rowf["ABFLIEFNR"].ToString().Trim();
                        
                        if (ABFLIEFNR == "") continue;
                        
                        if (processed.Contains(ABFLIEFNR + ";")) continue;
                        
                        processed += ABFLIEFNR+";";

                        rowtemp = INTEXDB.LIEFDAT.NewRow();
                        rowtemp["LSMANDANT"] = rowf["ABFMANDANT"];
                        rowtemp["LSLIEFNR"] = rowf["ABFLIEFNR"];                        
                        Result = INTEXDBC.QueryLIEFDAT(rowtemp, 2);
                        Return.Merge(Result);

                    }
                    string order= "";
                    string remark = "";
                    order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + "   ";
                    remark += GetCommentsFromTXTIND("AB3", order).Trim();
                    order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + row["ABPAUFPOS"].ToString();
                    remark += GetCommentsFromTXTIND("ABP", order).Trim();
                    row["ABPFILLER1"] = remark;
                #endregion
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }

    }
}